// pages/mv/mv.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    url: '',
    mvData: {},
    list: [],
    danmu:[
      {
        text: '这是一条弹幕',
        color: '#f00',
        time: 1
      }
    ]
  },

  timeUp: function(e) {
    var current = e.detail.currentTime
    current = Math.floor(current) + 1
    this.setData({
      current: current
    })
  },

  submit: function (e) {
    var text = e.detail.value.danmu
    console.log(text)
    var obj = {}
    obj.text = text
    // # + 16进制数字
    var str = '0123456789abcdef'
    var color = '#'
    for(let i = 0; i < 6; i++){
      var num = Math.floor(Math.random() * 16)
      color += str[num]
    }
    obj.color = color
    obj.time = this.data.current
    var danmu = this.data.danmu
    wx.setStorage({
      data: danmu,
      key: 'danmu',
    })
    danmu.push(obj)
    this.setData({
      danmu: danmu
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id
    this.setData({
      id: id
    })
    // 获取本地弹幕列表
    wx.getStorage({
      key: 'danmu',
      success: (res) => {
        console.log(res)
        this.setData({
          danmu: res.data
        })
      }
    })
    // mv视频url
    wx.request({
      url: 'http://127.0.0.1:3000/mv/url?id=' + id,
      success: (res) => {
        var url = res.data.data.url
        this.setData({
          url: url
        })
      }
    })
    // mv详情
    wx.request({
      url: 'http://127.0.0.1:3000/mv/detail?mvid=' + id,
      success: (res) => {
        var mvData = res.data.data
        var playCount = mvData.playCount
        if(playCount >= 1000){
          playCount = (playCount / 10000).toFixed(1) + '万'
        }
        mvData.playCount = playCount
        this.setData({
          mvData: mvData
        })
      }
    })
    // 相关mv
    wx.request({
      url: 'http://127.0.0.1:3000/related/allvideo?id=' + id,
      success: (res) => {
        var list = res.data.data
        // console.log(list)
        this.setData({
          list: list
        })
      }
    })
  },

  toVideo:function(e){
    var vid = e.currentTarget.dataset.vid
    wx.navigateTo({
      url: '/pages/video/video?vid=' + vid,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})